package com.gp.library.utils;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import static android.os.Build.VERSION;
import static android.os.Build.VERSION_CODES;

/**
 * 描述: Activity 管理工具类( 需要android 4.0系统(14) )
 **/

public class ActivityManagerUtils {
    public static final String TAG = "ActivityManagerUtils";

    private static ActivityManagerUtils activityManagerUtils;

    private ActivityManagerUtils() {
    }

    public static ActivityManagerUtils getInstance() {
        if (activityManagerUtils == null) {
            synchronized (ActivityManagerUtils.class) {
                if (activityManagerUtils == null) {
                    activityManagerUtils = new ActivityManagerUtils();
                }
            }
        }
        return activityManagerUtils;
    }


    /**
     * 管理Activity的list集合
     */
    private static List<Activity> mActivitys = Collections.synchronizedList(new LinkedList<Activity>());

    public List<Activity> getActivitys() {
        return mActivitys;
    }

    /**
     * 添加一个activity到list里
     */
    public void pushActivity(Activity activity) {
        if (mActivitys == null) {
            LogUtils.i(TAG, "pushActivity: mActivitys == null");
            return;
        }
        mActivitys.add(activity);
        LogUtils.i(TAG, "pushActivity: " + activity.getComponentName().getClassName() + " size:" + mActivitys.size());
    }

    /**
     * 从list里删除一个activity
     */
    public void popActivity(Activity activity) {
        if (mActivitys == null) {
            LogUtils.i(TAG, "popActivity:  mActivitys == null");
            return;
        }
        mActivitys.remove(activity);
        LogUtils.i(TAG, "popActivity: " + activity.getComponentName().getClassName() + "  size:" + mActivitys.size());
    }

    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls) {
        if (mActivitys == null || mActivitys.isEmpty()) {
            return;
        }
        List<Activity> activityList = new ArrayList<>();
        for (Activity activity : mActivitys) {
            if (activity != null && activity.getClass().equals(cls)) {
                activityList.add(activity);
            }
        }
        mActivitys.removeAll(activityList);
        for (int i = 0; i < activityList.size(); i++) {
            Activity activity = activityList.get(i);
            if (activity != null) {
                activity.finish();
            }
        }
    }


    /**
     * 结束指定类名的 Activity 集合
     */
    public void finishActivityCollection(@NonNull List<Class<?>> classList) {
        if (mActivitys == null || mActivitys.isEmpty()) {
            return;
        }
        List<Activity> needFinishedActivityList = new ArrayList<>();
        for (int i = 0; i < classList.size(); i++) {
            Class<?> aClass = classList.get(i);
            for (Activity activity : mActivitys) {
                if (activity != null && activity.getClass().equals(aClass)) {
                    if (!needFinishedActivityList.contains(activity)) {
                        needFinishedActivityList.add(activity);
                    }
                }
            }
        }
        mActivitys.removeAll(needFinishedActivityList);
        for (int j = 0; j < needFinishedActivityList.size(); j++) {
            Activity activity = needFinishedActivityList.get(j);
            if (activity != null) {
                activity.finish();
            }
        }
    }


    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        if (mActivitys == null) {
            return;
        }
        List<Activity> needFinishedActivityList = new ArrayList<>();
        for (int i = 0; i < mActivitys.size(); i++) {
            for (Activity activity : mActivitys) {
                if (activity != null) {
                    if (!needFinishedActivityList.contains(activity)) {
                        needFinishedActivityList.add(activity);
                    }
                }
            }
        }
        mActivitys.removeAll(needFinishedActivityList);
        LogUtils.i(TAG, "needFinishedActivityList :" + needFinishedActivityList);
        for (int j = 0; j < needFinishedActivityList.size(); j++) {
            Activity activity = needFinishedActivityList.get(j);
            if (activity != null) {
                activity.finish();
            }
        }
    }

    public void registerActivityListener(Application app) {
        if (VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH) {
            app.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
                @Override
                public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                    pushActivity(activity);
                }

                @Override
                public void onActivityStarted(Activity activity) {

                }

                @Override
                public void onActivityResumed(Activity activity) {
                    LogUtils.i(TAG, "onActivityResumed: " + activity.getComponentName().getClassName());
                }

                @Override
                public void onActivityPaused(Activity activity) {
                    LogUtils.i(TAG, "onActivityPaused: " + activity.getComponentName().getClassName());
                }

                @Override
                public void onActivityStopped(Activity activity) {
                    LogUtils.i(TAG, "onActivityStopped: " + activity.getComponentName().getClassName());
                }

                @Override
                public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
                }

                @Override
                public void onActivityDestroyed(Activity activity) {
                    if (null == mActivitys || mActivitys.isEmpty()) {
                        return;
                    }
                    if (mActivitys.contains(activity)) {
                        popActivity(activity);
                    }
                }
            });
        }
    }

    public void exitSystem() {
        finishAllActivity();
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

    public void turn2Activity(Context context, Class<?> cls) {
        turn2Activity(context, cls, null);
    }

    public void turn2Activity(Context context, Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(context, cls);
        if (bundle != null)
            intent.putExtras(bundle);
        context.startActivity(intent);
    }
}